/**
 *  @file Semaphore.h
 */
#ifndef _SEMAPHORE_H_
#define _SEMAPHORE_H_

#include "../common/BaseObject.h"

namespace cppflib
{

class CFString;

namespace threading
{

/**
 *  A semaphore class. On Linux side, it uses the POSIX semaphore instead of System V
 */
class _DLLAPI Semaphore : public BaseObject
{
private:
    ptrany_t pSem;         ///< pointer to semaphore object
    u32_t maxCount;        ///< maximum count of the semaphore
    bool isFirstInst;    ///< flag to indicate whether this semaphore is the first instance created
    CFString * pName;      ///< semaphore name if any

    Semaphore(void);
    void ReleaseBeforeThrowing();

public:
    explicit Semaphore(u32_t maxCount);
    Semaphore(pcwstr_t name, u32_t maxCount);
    virtual ~Semaphore(void);

    bool IsFirstInstance(void);
    void Wait(void);
    bool Wait(int timeout);
    bool Post(void); 
    void Close(void);
};


} // end of namespace threading


} // end of namespace cppflib


#endif
